//
//  PictureWallDetailController.m
//  WindRunner
//
//  Created by leeqb on 6/22/15.
//  Copyright (c) 2015 poweropt. All rights reserved.
//

#import "PictureWallDetailController.h"

@implementation PictureWallDetailController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if([self.data[@"userid"] integerValue] == [[AuthHelper getCurrentUser][@"id"] integerValue]) {
        _deleteButton.hidden = NO;
    } else {
        _deleteButton.hidden = YES;
    }
    
    CGRect rectImageView = _imageView.frame;
    rectImageView.origin.y = (SCREEN_HEIGHT - rectImageView.size.height + 64) / 2;
    _imageView.frame = rectImageView;
    
    NSString *imageUrl = [NSString stringWithFormat:@"%@%@", IMAGE_URL_PREFIX, self.data[@"url"]];
    [_imageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage getDefaultImageInSize:_imageView.frame.size]];
}

- (IBAction)deleteCurrent:(UIButton *)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                        message:LocalStringEvent(@"CDETAIL_CONFIRM_DELETE")
                                                       delegate:self
                                              cancelButtonTitle:LocalStringCommon(@"ALERT_CANCEL")
                                              otherButtonTitles:LocalStringCommon(@"ALERT_OK"), nil];
    alertView.tag = 1;
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 1) {
        if(buttonIndex == 1) {
            NSDictionary *requestData = @{@"userid":[AuthHelper getCurrentUser][@"id"],
                                          @"picid":self.data[@"id"]};
            FMRequestObject *requestObj = [[FMRequestObject alloc] initWithRUL:BASE_API_URL_WINDRUNNER
                                                                     interface:@"delpicture"
                                                                        params:requestData];
            [FMToast showLoading:LocalStringCommon(@"OPER_DOING")];
            [FMNetwork request:requestObj showMask:NO success:^(NSDictionary *data) {
                [FMToast showMessage:LocalStringCommon(@"OPER_SUCC")];
                [self.navigationController popViewControllerAnimated:YES];
            } falied:^(NSError *error) {
                [FMToast showMessage:LocalStringCommon(@"OPER_FAIL")];
            }];
        }
    } else {
        if(buttonIndex == 1) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [FMToast showLoading:LocalStringCommon(@"OPER_DOING")];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [FMToast hide];
                    [FMToast showMessage:LocalStringFind(@"REPORT_SUCCESS")];
                });
            });
        }
    }
}

- (IBAction)tapHandler:(UITapGestureRecognizer *)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:nil
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:@"举报", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0) {
        UIAlertView *alertForCancel = [[UIAlertView alloc] initWithTitle: LocalStringFind(@"REPORT_ALERT_TITLE")
                                                                 message: nil
                                                                delegate: self
                                                       cancelButtonTitle: LocalStringCommon(@"ALERT_CANCEL")
                                                       otherButtonTitles: LocalStringCommon(@"ALERT_OK"), nil];
        alertForCancel.alertViewStyle = UIAlertViewStylePlainTextInput;
        alertForCancel.tag = 2;
        [alertForCancel show];
    }
}

#pragma mark UIAlertViewDelegate
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    if(alertView.tag == 1) {
        return YES;
    } else {
        UITextField *txt = [alertView textFieldAtIndex:0];
        NSString *inputContent = [NSString formatString:txt.text];
        if([inputContent isEqualToString:@""]) {
            return NO;
        } else {
            return YES;
        }
    }
}

@end
